package com.jie.filter;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.jie.common.BaseContext;
import com.jie.utility.JwtUtils;
import com.jie.utility.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.StringBuilders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.RequestHeader;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

@WebFilter(urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {
//    路径匹配器
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void doFilter( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String uri = request.getRequestURI();
        String[] urls = new String[]{
                "/rear-end/admin/login",
                "/rear-end/admin/logout",
                "/user/sendMsg",
                "/user/sign",
                "/user/login",
                "/user/logout",
                "/address/findById",
                "/user/randomCode"
        };
        boolean check = check(urls, uri);
        if (check) {
            filterChain.doFilter(request, response);
            return;
        }

        String jwt = request.getHeader("Authorization");
        if(jwt!=null){
            R r = JwtUtils.parseJwt(jwt);
            if(r.getCode()==1){
                Map map = r.getMap();
                Integer adminId= (Integer) map.get("adminId");
                String key=null;
                Integer userId= (Integer) map.get("userId");
                if(adminId!=null){
                    key ="adminId:"+adminId;
                }
                if(userId!=null){
                    key="userId:"+userId;
                }
                if(StringUtils.isNotEmpty(key)){
                    String jwt1 = (String) redisTemplate.opsForValue().get(key);
                    if(jwt.equals(jwt1)){
                        filterChain.doFilter(request, response);
                        return;
                    }else {
                        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
                        return ;
                    }
                }

            }

        }
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return;
    }


    public boolean check(String[] urls, String url) {
        for (String s : urls) {
            boolean match = PATH_MATCHER.match(s, url);
            if (match) {
                return true;
            }
        }
        return false;
    }
}
